<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
    <script src="https://unpkg.com/vuex@next"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      // import {createApp} from 'vue'
      // import {createStore} from 'vuex'
      const { createApp } = Vue;
      const { createStore, mapMutations } = Vuex;

      const INCREMENTN = "INCREMENTN";

      const store = createStore({
        state: {
          count: 0,
        },
        mutations: {
          increment(state) {
            state.count++;
          },
          incrementN(state, { amount }) {
            state.count = state.count + amount;
          },
          [INCREMENTN](state, { amount }) {
            // mutate state
            state.count = state.count + amount;
          },
        },
      });

      const app = createApp({
        template: `
                <div>{{ $store.state.count }}</div>
                <button @click="$store.commit('increment')">Add</button>
                <button @click="$store.commit('incrementN',{amount: 10})">AddN</button>
                <button @click="$store.commit({type: 'incrementN',amount: 10})">AddN</button>
                <button @click="increment()">increment()</button>
                <button @click="incrementN({amount: 200})">incrementN({amount: 200})</button>
                <button @click="add()">add()</button>
            `,
        methods: {
          ...mapMutations([
            "increment", // map `this.increment()` to `this.$store.commit('increment')`

            // `mapMutations` also supports payloads:
            "incrementN", // map `this.incrementBy(amount)` to `this.$store.commit('incrementBy', amount)`
          ]),
          ...mapMutations({
            add: "increment", // map `this.add()` to `this.$store.commit('increment')`
          }),
        },
        mounted() {
          setTimeout(() => {
            this.$store.commit(INCREMENTN, { amount: 500 });
          }, 1000);
        },
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>
